Imports OpenNETCF.Net

'//****************************************************
'//NOTE: If you would like to enable connection manager you must have Smart Device Framework installed 
'//      on your development machine.  Once you have installed the SDF uncomment the define below
'****************************************************

#Const SDF_CONMGR = False


Public Class MainForm
#If SDF_CONMGR Then
    Private connManager As ConnManager
#End If
    Private rasEntries As RasEntryCollection
    Private devices() As DeviceInfo

    Public Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        'Populate listbox
        LoadEntries(lstEntries)

#If SDF_CONMGR Then
            'Create a new connection manager.  NOTE: this is no longer in the RAS Library but is available in the Smart Device Framework (www.smartdeviceframework.com)
            connManager = New ConnectionManager()
#End If

    End Sub

    Private Sub tabControl1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tabControl1.SelectedIndexChanged
        Select Case tabControl1.SelectedIndex
            Case 0
                LoadEntries(lstEntries)
            Case 1
                LoadDevices()
            Case 2
                LoadConnections()
        End Select
    End Sub
    
    Private Sub menuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles miExit.Click
        Application.Exit()
    End Sub
    Private Sub button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button7.Click
#If SDF_CONMGR Then
            Dim stat As ConnectionStatus =  connManager.Status 
            listBox1.Items.Add(String.Format("{0} = {1:X}", stat, CType(stat, Integer)))
#End If

    End Sub
    Private Sub button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button6.Click
#If SDF_CONMGR Then
            connManager.Connect(); '"http://www.microsoft.com", 0, 0, 30000)
#End If
    End Sub
    Private Sub button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button5.Click
#If SDF_CONMGR Then
            connManager.RequestDisconnect()
#End If
    End Sub
    Private Sub lstEntries_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstEntries.SelectedIndexChanged
        Dim enTry As RasEntry = rasEntries(lstEntries.SelectedIndex)
        txtPhone.Text = enTry.PhoneNumber
        txtAreaCode.Text = enTry.AreaCode
        txtDevice.Text = enTry.DeviceName
        txtIPAddress.Text = enTry.IPAddress
        txtDns.Text = enTry.IPAddressDns
    End Sub
    Private Sub cmdLoadEntries_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLoadEntries.Click
        LoadEntries(lstEntries)
    End Sub
    Private Sub cmdDial_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDial.Click
        Dim entry As RasEntry = rasEntries(lstEntries.SelectedIndex)
        AddHandler entry.RasStatus, AddressOf entry_RasStatus
        cmbStatus.Items.Clear()
        entry.Dial(True)
    End Sub

    Private Sub cmdCreate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdCreate.Click
        Dim rasEnt As RasEntry = New RasEntry()
        rasEnt.Name = txtName.Text
        If (chkOptions.Checked) Then
            rasEnt.Options = (rasEnt.Options Or CType(ConnectionOptions.SpecificIpAddr, Integer))
        Else
            rasEnt.Options = (rasEnt.Options Or CType(ConnectionOptions.UseCountryAndAreaCodes, Integer))
        End If
        rasEnt.CountryCode = 1
        rasEnt.AreaCode = txtNewAreaCode.Text
        rasEnt.PhoneNumber = txtNewPhone.Text
        rasEnt.DeviceName = devices(cmbDevices.SelectedIndex).DeviceName
        rasEnt.DeviceType = devices(cmbDevices.SelectedIndex).DeviceType
        rasEnt.IPAddress = txtNewIPAddress.Text
        rasEnt.IPAddressDns = txtNewDNS.Text

        Dim dialParams As RasDialParams = New RasDialParams()

        dialParams.UserName = txtUser.Text
        dialParams.Password = txtPassword.Text
        rasEnt.Options = (rasEnt.Options Or CType(ConnectionOptions.UseLogonCredentials, Integer))
        Ras.CreateEntry(rasEnt, dialParams)

    End Sub

    Private Sub entry_RasStatus(ByVal hConn As Integer, ByVal State As RasConnState, ByVal ErrorCode As RasError)
        Try
            cmbStatus.Items.Add(State.ToString() + " " + ErrorCode.ToString())
            cmbStatus.SelectedIndex = cmbStatus.Items.Count - 1
        Catch ex As ObjectDisposedException
            'ignore this exception
        End Try

    End Sub

    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
        Dim dialParams As RasDialParams = New RasDialParams()

        'Get the entry
        Dim enTry As RasEntry = rasEntries(lstEntries.SelectedIndex)
        AddHandler enTry.RasStatus, AddressOf entry_RasStatus
        enTry.Options = (enTry.Options Or CType(ConnectionOptions.DialAsLocalCall, Integer))
        enTry.Options = (enTry.Options Or CType(ConnectionOptions.SpecificIpAddr, Integer))

        'Clear the status combobox
        cmbStatus.Items.Clear()
        'Dial
        enTry.Dial(True, dialParams)
    End Sub

    Private Sub cmdHangUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdHangUp.Click
        Dim enTry As RasEntry = rasEntries(lstEntries.SelectedIndex)
        enTry.Hangup()
    End Sub

    Private Sub button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button3.Click
        Dim enTry As RasEntry = rasEntries(lstEntries.SelectedIndex)
        Ras.DeleteEntry(enTry)
    End Sub

    Private Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
        Dim conn() As RasConnection = Ras.ActiveConnections
        If Not conn Is Nothing Then
            MessageBox.Show(conn(0).Name + " " + conn.Length.ToString())
        End If
    End Sub

    Private Sub connManager_ConnectionStateChanged(ByVal sender As Object, ByVal e As EventArgs)
#If SDF_CONMGR Then
            listBox1.Items.Add(String.Format("{0}", connManager.Status))
#End If
    End Sub

    Private Sub LoadConnections()
#If SDF_CONMGR Then
            connManager.ConnectionStateChanged += New EventHandler(connManager_ConnectionStateChanged)


            Dim dest As DestinationInfo
            For Each dest In connManager.EnumDestinations()
                lbDestinations.Items.Add(New DestinationInfoListItem(dest))
            Next
            LoadEntries(lbEntries)
#End If
    End Sub

    Private Sub LoadEntries(ByVal lstList As ListBox)
        rasEntries = Ras.Entries
        lstEntries.Items.Clear()
        Dim i As Integer
        For i = 0 To rasEntries.Count - 1 Step i + 1
            lstList.Items.Add(rasEntries(i).Name)
        Next
        lstList.SelectedIndex = 0

    End Sub

    Private Sub LoadDevices()
        devices = Ras.Devices
        cmbDevices.Items.Clear()
        Dim i As Integer
        For i = 0 To devices.Length - 1 Step i + 1
            cmbDevices.Items.Add(devices(i).DeviceName)
        Next
        If cmbDevices.Items.Count > 0 Then
            cmbDevices.SelectedIndex = 0
        End If
    End Sub

    Private Class DestinationInfoListItem
#If SDF_CONMGR Then
            Dim m_di As DestinationInfo
            Public  Sub New(ByVal di As DestinationInfo)
                m_di = di
            End Sub
            Public Overrides Function ToString() As String
                Return m_di.Description
            End Function
#End If
    End Class


End Class
